package net.starwifi.duobao.web;

import net.starwifi.duobao.entity.Ggljp;

import net.starwifi.duobao.mvc.Filter;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.mvc.Order;
import net.starwifi.duobao.mvc.PageView;
import net.starwifi.duobao.service.GglhdService;
import net.starwifi.duobao.service.GgljpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller - member.
 *
 * @author ruoo
 * @version 1.0
 */
@Controller("adminggljpController")
@RequestMapping("/ggljp")
public class GgljpController extends BaseController {

    @Autowired
    GgljpService ggljpService;
    @Autowired
    GglhdService gglhdService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(PageView pageView, Long Id,ModelMap model,HttpServletRequest request) {
        pageView.filterQuery(request);
        pageView.addFilter(Filter.eq("gglhdid", Id));
        pageView.addOrder(Order.desc("id"));
        model.addAttribute("gglhd", gglhdService.find(Id));
        model.addAttribute("page", ggljpService.getScrollData(pageView));
        return "/ggljp/list";
    }
    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Long id, ModelMap model) {
        Ggljp  ggljp =new Ggljp();
        ggljp.setGglhdid(id);
        model.addAttribute("entity", ggljp);
        return "/ggljp/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Ggljp ggljp, RedirectAttributes redirectAttributes) {
        if (!isValid(ggljp)) {
            return ERROR_VIEW;
        }
            ggljp.setKc(ggljp.getNum());

        ggljpService.save(ggljp);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/ggljp/list?Id="+ggljp.getGglhdid();
    }


    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model) {
        model.addAttribute("entity", ggljpService.find(id));
        return "/ggljp/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Ggljp ggljp, RedirectAttributes redirectAttributes) {
        Ggljp pGgljp=ggljpService.find(ggljp.getId());
        ggljpService.update(ggljp);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/ggljp/list?Id="+ggljp.getGglhdid();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    Message delete(Long[] ids) {
        ggljpService.delete(ids);
        return SUCCESS_MESSAGE;
    }



}
